package com.uduemc.biso.master.backend.validator.impl;

import java.io.IOException;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

import org.springframework.util.StringUtils;

import com.uduemc.biso.core.extities.center.MasterUser;
import com.uduemc.biso.master.backend.service.MastermanageService;
import com.uduemc.biso.master.backend.validator.MasterUserUsernameCreate;
import com.uduemc.biso.master.config.SpringContextUtils;

public class MasterUserUsernameCreateValid implements ConstraintValidator<MasterUserUsernameCreate, String> {

	@Override
	public void initialize(MasterUserUsernameCreate constraintAnnotation) {

	}

	@Override
	public boolean isValid(String value, ConstraintValidatorContext context) {
		if (StringUtils.isEmpty(value)) {
			return false;
		}
		MastermanageService mastermanageServiceImpl = SpringContextUtils.getBean("mastermanageServiceImpl",
				MastermanageService.class);
		MasterUser byUsername = null;
		try {
			byUsername = mastermanageServiceImpl.getByUsername(value);
		} catch (IOException e) {
			e.printStackTrace();
		}

		if (byUsername == null) {
			return true;
		}
		return false;
	}

}
